<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item>添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <el-tree
          :data="list"
          :props="defaultProps"
          :default-expand-all="true"
        >
          <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
          <!--
      什么时候用到作用域插槽？父组件中如果想使用子组件中的数据进行自定义内容的渲染 (table 单元格数据渲染)
    -->
          <template slot-scope="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item @click.native="delPart(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>

        <el-dialog :title="isEdit ? '编辑' : '添加'" :visible.sync="showDialog">
          <AddorEdit
            v-if="showDialog"
            :pid="curId"
            :is-edit="isEdit"
            :existed-code-list="existedCodeList"
            :existed-name-list="cExistedName"
            @close="hCancel"
            @update-depart="hUpdateDepart"
          />
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getDepartments, delDepartment } from '@/api/departments.js'
import { tranListToTreeData } from '@/utils/index'
import AddorEdit from './AddorEdit'

export default {
  components: {
    AddorEdit
  },
  data() {
    return {
      curId: '', // 当前点击哪个部门的id
      isEdit: false, // 是否处于编辑状态
      showDialog: false, // 当showDislog变化时,v-if会导致这个组件销毁和创建
      existedCodeList: [], // code
      originList: [], // 获取部门信息
      list: [
        // {
        //   name: '财务部',
        //   manager: '小红',
        //   children: [
        //     {
        //       name: '财务核算部',
        //       manager: '小明'
        //     },
        //     {
        //       name: '税务核算部',
        //       manager: '小美'
        //     }
        //   ]
        // }
      ],
      defaultProps: {
        label: 'name',
        children: 'children'
      }
    }
  },
  computed: {
    cExistedName() {
      if (this.isEdit) { // 是否处于编辑状态
        // 编辑时部门名称
        const curNode = this.originList.find(item => item.id === this.curId)
        console.log(curNode)
        // 筛选出来的部门名称
        return this.originList.filter(item => item.pid === curNode.pid).map(item => item.name)
      } else {
        return this.originList.filter(item => item.pid === this.curId).map(item => item.name)
      }
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    // 请求部门信息
    async loadDepartments() {
      const res = await getDepartments()
      console.log(res)
      this.list = tranListToTreeData(res.data.depts)
      console.log(this.list)
      this.existedCodeList = res.data.depts.map(item => item.code) // 拿到每个部门的code
      // 获取到部门信息时,给它赋值
      this.originList = res.data.depts.map(item => { return { id: item.id, pid: item.pid, name: item.name } })
      console.log(this.existedCodeList)
    },
    // 收到子组件中的update-depart事件
    hUpdateDepart() {
      // 关闭弹层，再次更新数据
      this.showDialog = false
      this.loadDepartments()
    },
    // 删除
    delPart(id) {
      this.$confirm('你确认要进行删除么?', '温馨提示').then(async() => {
        console.log('aa')
        // 删除操作成功
        await delDepartment(id)
        // 添加提示
        this.$message.success('删除操作成功')
        // 更新数据
        this.loadDepartments()
      }).catch(error => {
        console.log(error)
        this.$message('已取消删除')
      })
    },
    // 添加时传的id
    hAdd(id) {
      this.showDialog = true
      this.curId = id
      console.log(id)
      // 添加时不处于编辑状态
      this.isEdit = false
    },
    // 用户点击了编辑
    hEdit(id) {
      // 1. 弹层出来
      this.showDialog = true
      // 2. 表单中要显示详情数据
      // 把id传给子组件，在子组件中去获取详情
      this.curId = id
      console.log(this.curId)
      // 3. 传递一个标识给子组件，告诉它我们现在是编辑状态
      this.isEdit = true
    },
    // 收到子组件中的close事件
    hCancel() {
      this.showDialog = false
    }
  }
}
</script>

<style>

</style>
